#include "recorder.h"
#include "main.h"

#define  WRITE_STRING(str)  doc.write(codec1->fromUnicode(str));
#define  WRITE_NUMBER(num)  doc.write(codec1->fromUnicode(QString::number(num)));
#define  WRITE(str)         doc.write(codec1->fromUnicode(trUtf8(str)))

recorder::recorder(QObject *parent) :
    QObject(parent)
{
    open_record_file();
    timer=new QTimer();
    QObject:: connect( timer, SIGNAL(timeout()), SLOT(record()) );
    rn=0;
    print_type_tmp=whole_machine_parameter.realtime_print_record;
}

 void recorder::open_record_file()
{
     if(doc.isOpen())  doc.close();

     if(whole_machine_parameter.realtime_print_record)
     {
         doc.setFileName(COM4);
         codec1 = QTextCodec::codecForName("GB2312");
     }
     else
     {
         doc.setFileName(RECORD_DOC);
         codec1 = QTextCodec::codecForName("UTF8");
     }
     doc.open(QIODevice::ReadWrite | QIODevice::Text |QIODevice::Unbuffered);

}

 void recorder::record()
{
     if(print_type_tmp!=whole_machine_parameter.realtime_print_record)
     {
         print_type_tmp=whole_machine_parameter.realtime_print_record;
         open_record_file();
     }
    rec[rn].currenttime=QTime::currentTime();
    rec[rn].termperature=TEM;
    rec[rn].pressure=PSU;
    realtime_print();
    rn++;

}

void recorder::clear()
{
    rn=0;
    cut[2]=0;cut[3]=0;cut[4]=0;
    i=0;
}

 void recorder::stage_cut(uchar n)
 {
     if(n==4 && (rn-cut[3]< recdnr)) record();
     cut[n]=rn;
 }
 void recorder::auto_record()
{
  timer->start(60000);
}

 void recorder::stop_auto_record()
{
 timer->stop();
}

void recorder::realtime_print()
{
    if(whole_machine_parameter.realtime_print_record)
    {
       if(rn==0)  {print_header();i=0;}
       else
       {
           if(sterilizing) print_sterilization_data();
           else
           {
               print_error();
               doc.close();
               doc.setFileName(RECORD_DOC);
               codec1 = QTextCodec::codecForName("UTF8");
           }

       }
       if(doc.isOpen()) doc.flush();

    }


}

void recorder::print_header()
{
    // doc.resize(0) ;//error
   if(!whole_machine_parameter.realtime_print_record)
   {
    doc.remove();
    if(doc.isOpen()) doc.close();
    doc.open(QIODevice::ReadWrite | QIODevice::Text |QIODevice::Unbuffered | QIODevice::Truncate );
   }

    WRITE("   广州市豪尔生医疗设备有限公司");
    WRITE("\n产品编号：");   WRITE_STRING(whole_machine_parameter.serial_number)
    WRITE("     使用次数：");WRITE_NUMBER(whole_machine_parameter.used_times_counter)
    WRITE("\n开始时间：");   WRITE_STRING(starttime)
    WRITE("\n灭菌方式：");   WRITE_STRING(str_sterilization_type)
    WRITE("  操作者：");
    WRITE("\n灭菌温度：");    WRITE_NUMBER(sterilization_temperature)
    WRITE("     灭菌时间："); WRITE_NUMBER(sterilization_time)

    if(stype->is_pulsing_vacuum)
    {
        WRITE("\n脉动次数：");
        WRITE_NUMBER(pulsing_times)
        WRITE("       干燥次数：");
        WRITE_NUMBER(dry_times)
        }
    else  {
           WRITE("\n排冷次数：");
           WRITE_NUMBER(cold_exhaust_times)
        }


    WRITE("\n阶段:    时间      温度℃    压力kPa\n");

}
void recorder::print_error()
{
    WRITE("结束:   "); WRITE_STRING(QTime::currentTime().toString("hh:mm:ss"))
    WRITE("\n");

switch(breakdown)
{
case NO_ERROR:                                            break;
case LOW_TEM:              WRITE("灭菌失败：低温");     break;
case Vacuum_pump_NO_WATER: WRITE("灭菌失败：真空泵没有冷却水");break;
case Vacuum_pump_TimeOut:  WRITE("灭菌失败：真空泵运行超时");  break;
case LOWEST_WATER:         WRITE("灭菌失败：缺水");          break;
case DOOR_ERROR:           WRITE("灭菌失败：门开关故障");     break;
case OVERPRESSURE:         WRITE("灭菌失败：夹层超压");       break;
case MANUAL_STOP:          WRITE("手动停止灭菌");            break;
case PRESSURE_REACHED:
    WRITE("灭菌失败：恒温时夹层压力到位\n 请重新校准温度传感器");   break;
case OVERTIME:
    WRITE("灭菌失败：升温过程超时\n 检查手动排气阀是否关闭或联系维护人员！");   break;
}


}

void recorder::end_print()
{
    //rn=0;
    breakdown= NO_ERROR ;
    WRITE("\n\n");
    doc.copy(RECORD_DIR +starttime);
    doc.flush();
    if(whole_machine_parameter.realtime_print_record)
    {
        doc.close();
        doc.setFileName(COM4);
        codec1 = QTextCodec::codecForName("GB2312");
    }
    clear();
}

void recorder::print_sterilization_data()
{
if(i==0) (stype->is_pulsing_vacuum)? WRITE("脉动:  "):WRITE("排冷:   ");
else if(i==cut[2]) WRITE("升温:   ");
else if(i==cut[3]) WRITE("灭菌:   ");
else if(i==cut[4]) (stype->is_pulsing_vacuum)?WRITE("干燥:   "):WRITE("排气:   ");
else WRITE("       ");
if(whole_machine_parameter.realtime_print_record)
{
    WRITE_STRING(QTime::currentTime().toString("hh:mm:ss"))  WRITE("    ");
    doc.write(codec1->fromUnicode(QString::number(TEM,'f',1))); WRITE("    ");
    WRITE_NUMBER(PSU)
}
else
{
    WRITE_STRING(rec[i].currenttime.toString("hh:mm:ss"));  WRITE("    ");
    WRITE_STRING(QString::number(rec[i].termperature,'f',1)); WRITE("    ");
    WRITE_NUMBER(rec[i].pressure);
}
WRITE("\n");
i++;
}

void recorder::save()
{
    realtime_print();
    timer->stop();
    print_header();
    i=0;
    while(i<rn)
    {
    print_sterilization_data();
    }

    print_error();
    end_print();

}


